package middlewares

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

var AdminFilter = func(ctx *context.Context) {
	userInfo := ctx.Input.Session("userInfo")
	if userInfo == nil && !(len(ctx.Request.RequestURI) >= len("/admin/login") && ctx.Request.RequestURI[0:len("/admin/login")] == "/admin/login") {
			ctx.Redirect(302, "/admin/login")
	}
}

func AdminMiddleware() {
	beego.InsertFilter("/admin/*", beego.BeforeRouter, AdminFilter)
}
